﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace AusProject.OrderDataControls
{
    public partial class CheckDataBox : AusProject.OrderDataControls.DataBox
    {
        private const int TitleValueSpace = 3;
        private const int TitleValueSep = 2;
        private const int VMargin = 3;
        private const int HMargin = 3;

        private const string YesLabel = "[X]";
        private const string NoLabel = "[  ]";


        public CheckDataBox()
        {
            InitializeComponent();
            // assign font for the 1st time so that when user assign later, FontChanged event would be raised
            lblTitle.Font = txtTitle.Font = lblValue.Font = this.Font;

            Title = "CheckBox:";
            Mode = DataControlMode.Design;
            BorderMode = DataControlBorderMode.None;
            Editable = true;
            DefaultValue = "";
            Value = "";

            CheckInputBox_Resize(null, null);
        }

        public override string ToString()
        {
            return "CheckBox";
        }


        private void CheckInputBox_Paint(object sender, PaintEventArgs e)
        {
            switch (BorderMode)
            {
                case DataControlBorderMode.Full:
                    e.Graphics.DrawRectangle(borderPen, 0, 0, this.Width - 1, this.Height - 1);
                    if (lblTitle.Width > 0)
                    {
                        e.Graphics.DrawLine(borderPen, lblValue.Left - TitleValueSep, 0, lblValue.Left - TitleValueSep, this.Height - 1);
                    }
                    break;
                case DataControlBorderMode.TitleOnly:
                    if (lblTitle.Width > 0)
                    {
                        e.Graphics.DrawRectangle(borderPen, 0, 0, HMargin + lblTitle.Width + TitleValueSep, this.Height - 1);
                    }
                    break;
                case DataControlBorderMode.ValueOnly:
                    if (lblTitle.Width > 0)
                    {
                        e.Graphics.DrawRectangle(borderPen, lblValue.Left - TitleValueSep, 0, TitleValueSep + lblValue.Width + HMargin - 1, this.Height - 1);
                    }
                    else
                    {
                        e.Graphics.DrawRectangle(borderPen, 0, 0, this.Width - 1, this.Height - 1);
                    }
                    break;
                case DataControlBorderMode.None:
                    if (mode == DataControlMode.Design)
                    {
                        e.Graphics.DrawRectangle(borderDashPen, 0, 0, this.Width - 1, this.Height - 1);
                    }
                    break;
            }
        }

        private void CheckInputBox_Resize(object sender, EventArgs e)
        {
            lblTitle.Top = (this.Height - lblTitle.Height) / 2;
            txtTitle.Top = (this.Height - txtTitle.Height) / 2;
            lblValue.Top = (this.Height - lblValue.Height) / 2;
            lblValue.Width = this.Width - VMargin - (lblTitle.Width > 0 ? lblTitle.Width + TitleValueSpace : 0) - VMargin;
            chkValue.Top = (this.Height - chkValue.Height) / 2;
            chkValue.Left = lblValue.Left + lblValue.Width - chkValue.Width;
        }

        private void CheckInputBox_Enter(object sender, EventArgs e)
        {
            if (mode == DataControlMode.Design)
            {
                lblTitle_Click(null, null);
            }
            else
            {
                lblValue_Click(null, null);
            }
        }


        #region Title Property

        public override string Title
        {
            set
            {
                title = value;
                lblTitle.Text = title;
                txtTitle.Text = title;
                OnTitleChanged();
            }
        }


        private void lblTitle_Click(object sender, EventArgs e)
        {
            if (mode == DataControlMode.Design)
            {
                lblTitle.Visible = false;
                txtTitle.Visible = true;
                txtTitle.Focus();
                txtTitle.SelectAll();
            }
        }

        private void lblTitle_Resize(object sender, EventArgs e)
        {
            txtTitle.Size = lblTitle.Size;

            lblValue.Left = chkValue.Left = VMargin + (lblTitle.Width > 0 ? lblTitle.Width + TitleValueSpace : 0);
            lblValue.Height = lblTitle.Height;

            this.Width = lblValue.Left + lblValue.Width + VMargin;
            this.MinimumSize = new Size(lblTitle.Left + lblTitle.Width + TitleValueSep
                                            + Math.Max(TextRenderer.MeasureText(YesLabel, lblValue.Font).Width, TextRenderer.MeasureText(NoLabel, lblValue.Font).Width),
                                                    VMargin + lblTitle.Height + VMargin);
            this.Invalidate();
        }


        private void txtTitle_Enter(object sender, EventArgs e)
        {
            txtTitle.SelectAll();
        }

        private void txtTitle_Leave(object sender, EventArgs e)
        {
            lblTitle.Visible = true;
            txtTitle.Visible = false;
            Title = txtTitle.Text;
        }

        private void txtTitle_TextChanged(object sender, EventArgs e)
        {
            lblTitle.Text = txtTitle.Text;
        }

        #endregion Title Property


        #region Value Property

        protected override void SetValue(string value)
        {
            this.value = value;
            chkValue.Checked = (this.value.CompareTo("1") == 0);
            lblValue.Text = chkValue.Checked ? YesLabel : NoLabel;
        }


        private void lblValue_Click(object sender, EventArgs e)
        {
            if ((mode == DataControlMode.Running) && (editable))
            {
                lblValue.Visible = false;
                chkValue.Visible = true;
                chkValue.Focus();
            }
        }

        private void chkValue_CheckedChanged(object sender, EventArgs e)
        {
            this.value = chkValue.Checked ? "1" : "0";
            lblValue.Text = chkValue.Checked ? YesLabel : NoLabel;
        }

        private void chkValue_Leave(object sender, EventArgs e)
        {
            lblValue.Visible = true;
            chkValue.Visible = false;
        }

        #endregion Value Property


        protected override void SetEditable(bool value)
        {
            chkValue.Enabled = value;
        }

        protected override void SetDesignMode()
        {
            lblTitle.Cursor = Cursors.IBeam;
            lblValue.Cursor = Cursors.SizeAll;
            this.Invalidate();
        }

        protected override void SetRunningMode()
        {
            lblTitle.Cursor = Cursors.Arrow;
            lblValue.Cursor = Cursors.IBeam;
            this.Invalidate();
        }


        private void CheckInputBox_FontChanged(object sender, EventArgs e)
        {
            lblTitle.Font = txtTitle.Font = lblValue.Font = this.Font;

            lblTitle_Resize(null, null);
            this.Height = VMargin + txtTitle.Height + VMargin;
            CheckInputBox_Resize(null, null);
        }



        private void lblValue_MouseDown(object sender, MouseEventArgs e)
        {
            if (mode == DataControlMode.Design)
            {
                Point p = this.PointToClient(lblValue.PointToScreen(e.Location));
                this.OnMouseDown(new MouseEventArgs(e.Button, e.Clicks, p.X, p.Y, e.Delta));
            }
        }

        private void lblValue_MouseMove(object sender, MouseEventArgs e)
        {
            if (mode == DataControlMode.Design)
            {
                Point p = this.PointToClient(lblValue.PointToScreen(e.Location));
                this.OnMouseMove(new MouseEventArgs(e.Button, e.Clicks, p.X, p.Y, e.Delta));
            }
        }

        private void lblValue_MouseUp(object sender, MouseEventArgs e)
        {
            if (mode == DataControlMode.Design)
            {
                Point p = this.PointToClient(lblValue.PointToScreen(e.Location));
                this.OnMouseUp(new MouseEventArgs(e.Button, e.Clicks, p.X, p.Y, e.Delta));
            }
        }



        public override DataBoxInfo ExportDataBoxInfo()
        {
            return new DataBoxInfo(-1, DataBoxType.CheckDataBox, Title, Editable, BorderMode, DefaultValue, Left, Top, Width, Height, null, SummaryFieldId, Value, new DataBoxFont(this.Font.Name, this.Font.Size, this.Font.Bold, this.Font.Italic));
        }
    }
}
